﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.ComponentModel.Composition;

namespace Kooboo.Commerce.Repositories
{
    [Export(typeof(ICustomField))]
    public partial class ProductAttribute :  ICustomField
    {
        IQueryable<IProductCustomField> ICustomField.ProductCustomFields
        {
            get
            {
                return this.DataContext.ProductCustomFields
                    .Where(i => i.CustomFieldId == this.Id);

            }
        }

 
        List<string> _Selections_For_IProductAttribute;
        List<string> IProductAttribute.Selections
        {
            get
            {
                if(this._Selections_For_IProductAttribute == null)
                {
                    var list = new List<string>();
                    using(StringReader reader = new StringReader(this.Selections))
                    {
                        var item = reader.ReadLine();
                        while(item != null)
                        {
                            list.Add(item);
                            item = reader.ReadLine();
                        }
                    }

                    this._Selections_For_IProductAttribute = list;
                }

                return this._Selections_For_IProductAttribute;
            }
            set
            {
                this._Selections_For_IProductAttribute = value;
                if(value == null)
                {
                    this.Selections = null;
                }
                else
                {
                    this.Selections = string.Join(Environment.NewLine,value.ToArray());
                }
            }
        }

    }
}
